#include<linux/module.h>
#include<linux/sched.h>
#include<linux/pid.h>
#include<linux/completion.h>
#include<linux/wait.h>
#include<linux/list.h>
#include<linux/kthread.h>
MODULE_LICENSE("GPL");

static struct completion comple;
struct task_struct *old_thread;

int my_function(void *argc)
{
	int result=-1;
	wait_queue_head_t head;
	wait_queue_t data;
	printk("in the kernel thread function!\n");
	init_waitqueue_head(&head);
	init_waitqueue_entry(&data,current);
	add_wait_queue(&head,&data);
	schedule_timeout_uninterruptible(100);
	complete(&comple);
	printk("the value of done of the comple is:%d\n",comple.done);
	result=completion_done(&comple);
	printk("the return result of the completion_done is:%d\n",result);
	printk("the current pid is%d\n",current->pid);
	printk("the init function pid is:%d\n",old_thread->pid);
	printk("the state of the init function is:%ld\n",old_thread->state);

	//complete(&comple);
	printk("out the kernel thread function\n");
	return 0;
}
static int __init completion_done_init(void)
{
	char namefrm[]="complete.c";
	struct task_struct *result;
	wait_queue_t data;
	printk("into completion_done_init.\n");
	old_thread=current;
	result=kthread_create_on_node(my_function,NULL,-1,namefrm);
	printk("my_name_is_lizhi\n");
	wake_up_process(result);

	init_completion(&comple);
	init_waitqueue_entry(&data,result);
	__add_wait_queue_tail(&(comple.wait),&data);
	wait_for_completion(&comple);
	printk("the pid of result is:%d\n",result->pid);

	printk("the current pid is:%d\n",current->pid);
	printk("out completion_done_init.\n");
	return 0;
}
static void __exit completion_done_exit(void)
{
	printk("Goodbye completion_done!\n");
}
module_init(completion_done_init);
module_exit(completion_done_exit);
